package com.earl.eatbao.webMock;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.earl.eatbao.service.UserService;
import com.earl.eatbao.web.UserController;

//@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@ComponentScan(value = { "com.earl.eatbao.application"
		,"com.earl.eatbao.interceptor"})
@WebMvcTest(UserController.class)
public class UserWebMock {

	@Autowired
	private MockMvc mvc;

	// 模拟userService userService只需要实现接口,然后就行了
	@MockBean
	private UserService userService;

	@Test
	public void doLoginTest() throws Exception {
		this.mvc.perform(get("users/changePassword").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
				.andExpect(content().string("Honda Civic"));
	}
	@Test
	public void doLogin2Test() throws Exception {
		// RemoteService has been injected into the reverser bean
		given(this.userService.changePassword(123L, "1234", "12345")).willReturn(true);
		this.mvc.perform(get("users/changePassword").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
		.andExpect(content().string("Honda Civic"));
	}
	@Test
	public void exampleTest2() throws Exception {
		// RemoteService has been injected into the reverser bean
		given(this.userService.changePassword(123L, "1234", "12345")).willReturn(false);
		this.mvc.perform(post("/users/changePassword").param("id","123").param("newPassword", "12345").param("oldPassword", "1234").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
		.andExpect(content().string("Honda Civic"));
	}
}
